
//
//  DDEmoticon.swift
//  EmojiKeyboardTest
//
//  Created by dqw on 15/8/6.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class DDEmoticonPackage: NSObject {

    var id: String?
    var groupName: String?
    var emoticons: [DDEmoticon]?
    
    init(id: String, groupName: String = "") {
        self.id = id
        super.init()
    }
    
    class func loadPackages() -> [DDEmoticonPackage] {

        let path = bundlePath?.stringByAppendingPathComponent("emoticons.plist")
        
        
        let dict = NSDictionary(contentsOfFile: path!) as! [String: AnyObject]
        let arr = dict["packages"] as! [[String: AnyObject]]
        var arrM = [DDEmoticonPackage]()
        let package = DDEmoticonPackage(id: "", groupName: "最近").appendEmptyEmoticon()
        arrM.append(package)
        for dict2 in arr {
            let package = DDEmoticonPackage(id: dict2["id"] as! String).loadEmoticons().appendEmptyEmoticon()
            arrM.append(package)
        }
        return arrM
    }
    
    // FIXME:self 的首字母大写。
    private func loadEmoticons() -> Self {
        
        let subPath = bundlePath?.stringByAppendingPathComponent(id!).stringByAppendingPathComponent("info.plist")
        let dict = NSDictionary(contentsOfFile: subPath!) as! [String: AnyObject]
        groupName = dict["group_name_cn"] as? String
        
        let arr = dict["emoticons"] as! [[String: String]]
        var arrM = [DDEmoticon]()
        
        var index = 0
        for dict2 in arr {
            
            let emoticon = DDEmoticon(id: id!, dict: dict2)
            arrM.append(emoticon)
            if ++index == 20 {
                arrM.append(DDEmoticon(remove: true))
                index = 0
            }
            
        }
        emoticons = arrM
        return self
    }
    
    private func appendEmptyEmoticon()-> Self {
        
        
        
        if emoticons == nil {
            emoticons = [DDEmoticon]()
        }
        
        let count = (emoticons!.count) % 21
        
        
        if count > 0 || emoticons!.count == 0 {
            for _ in count..<20 {
                
                emoticons?.append(DDEmoticon(remove: false))
            }
            
            emoticons?.append(DDEmoticon(remove: true))
        }
        print(emoticons?.count)
        return self
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        
        return "id=\(id), groupName=\(groupName), emoticons=\(emoticons)"
    }
    
    
    
}
private var bundlePath: String? = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle")
class DDEmoticon: NSObject {
    
    var id: String?
    var chs: String?
    var png: String?
    var code: String? {
        didSet {
            let scanner = NSScanner(string: code!)
            var value: UInt32 = 0
            scanner.scanHexInt(&value)
            // FIXME: 结构体层层包装
            emoji = String(Character(UnicodeScalar(value)))
        }
    }
    var emoji: String?
    var imagePath: String? {
        
        if chs == nil {
            return ""
        }
       return bundlePath?.stringByAppendingPathComponent(id!).stringByAppendingPathComponent(png!)
    }
    
    var removeFlag: Bool = false
    init(remove: Bool) {
        removeFlag = remove
    }

    
    init(id: String, dict: [String: String]) {
        self.id = id
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        
        return "id=\(id), chs=\(chs), png=\(png), code=\(code), emoji=\(emoji), imagePath=\(imagePath)"
    }

}


